package Data_Structure.List.Demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @className: MyCard
 * @author: 芃
 * @description: 扑克牌模拟
 * @date: 2023/8/24 14:29
 */
public class MyCard {
    private static final String[] suits = {"♥","♦","♣","♠"};

    public static void main(String[] args) {
        List<Card> cards = createCard();
        shuffle(cards);
        for (Card card: cards) {
            System.out.println(card.toString());
        }
        ArrayList<Card> person1 = new ArrayList<>();
        ArrayList<Card> person2 = new ArrayList<>();
        ArrayList<Card> person3 = new ArrayList<>();
        //二维顺序表，顺序表中的每个元素都是一个顺序表
        ArrayList<ArrayList<Card>> person = new ArrayList<>();
        //三个人，每个人轮流揭5张牌,一张一张揭
        for (int i = 1; i <= 5; i++) {
            ArrayList<Card> tmp = new ArrayList<>(3);
            for (int j = 1; j <= 3; j++) {
                 tmp.add(cards.remove(0));
            }
            person1.add(tmp.get(0));
            person2.add(tmp.get(1));
            person3.add(tmp.get(2));
        }
        person.add(person1);
        person.add(person2);
        person.add(person3);
        for (ArrayList<Card> people: person) {
            for (Card card: people) {
                System.out.print(card + " ");
            }
            System.out.println();
        }
    }
    //构建一副牌
    public static List<Card> createCard(){
        ArrayList<Card> cards = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < suits.length; j++) {
                Card card = new Card(i,suits[j]);
                cards.add(card);
            }
        }
        return cards;
    }
    //洗牌
    private static void shuffle(List<Card> cards){
        int size = cards.size();
        for (int i = size - 1; i > 0 ; i--) {
            Random random = new Random();
            int rand = random.nextInt(i);
            swap(cards,i,rand);
        }
    }
    private static void swap(List<Card> cards,int i,int j){
        Card tmp = cards.get(i);
        cards.set(i,cards.get(j));
        cards.set(j,tmp);
    }
}
